<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }}条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog = true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table :data="list" border :default-sort="{ prop: 'timeOfEntry', order: 'descending' }">
          <el-table-column label="序号" type="index" />
          <el-table-column
            label="姓名"
            prop="username"
          />
          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{row}">
              {{ formatData(row.formOfEmployment ) }}
            </template>

          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />

          <el-table-column label="入职时间" sortable prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态" prop="" /> -->
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <el-dialog title="新增或修改" :visible.sync="showDialog">
        <empDialog @close="showDialog =false" @success="hSuccess" />

      </el-dialog>
    </div>
  </div>
</template>
<script>
import employeesEnum from '@/constant/employees'
import { getEmployees, delEmployee } from '@/api/employees'
import empDialog from './empDialog.vue'
console.log(employeesEnum, 'employeesEnum')
console.log(employeesEnum.hireType, 'employeesEnum.hireType')
// const hireType = {}
// employeesEnum.hireType.forEach(item => {
//   hireType[item.id] = item.value
// })
const hireType = employeesEnum.hireType.reduce((acc, item) => {
  acc[item.id] = item.value
  return acc
}, {})
export default {
  components: { empDialog },
  data() {
    return {
      list: [],
      page: 1,
      size: 10,
      total: 0,
      showDialog: false
    }
  },
  created() {
    this.loadEmployees()
  },
  methods: {
    hSuccess() {
      this.total++
      const pageMax = Math.ceil(this.total / this.page)
      this.page = pageMax
      this.loadEmployees()
      this.showDialog = false
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.size = val
      this.loadEmployees()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.loadEmployees()
    },
    formatData(id) {
      // 目标传过来的id 1:2 转为正式||非正式
      // console.log(id, 'id')
      // return employeesEnum.hireType.find(item => item.id === id).value
      return hireType[id] || '未知'
    },
    async loadEmployees() {
      try {
        const res = await getEmployees({ page: this.page, size: this.size })
        console.log(res, 'getEmployees')
        if (res.code !== 10000) return this.$message.error(res.message)
        // this.$message.success(res.message)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    async hDel(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delEmployee(id)
        // console.log('delEmployee', res)
        if (this.list.length === 1) {
          this.page--
          if (this.list.length <= 0) {
            this.page === 1
          }
        }
        if (res.code !== 10000) return this.$message.error(res.message)

        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loadEmployees()
      }).catch((error) => {
        this.$message.error(error)
      })
    }
  }
}

</script>
